package org.alino.core.shiro.session;

import org.alino.core.utils.LoggerUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.SessionKey;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;

import java.io.Serializable;

/**
 * @Desc TODO
 * @Author by Brant
 * @Date 2017/1/19
 */
public class OverrideWebSessionManager extends DefaultWebSessionManager {
    /**
     * 解决总是抛出
     * java.lang.IllegalStateException: org.apache.shiro.session.UnknownSessionException: There is no session with id [64e3ab0e-a9ac-4671-bf90-0c5123aa78ba]
     * @param sessionKey
     * @return
     */
    @Override
    protected Session retrieveSession(SessionKey sessionKey) {
        Serializable sessionId = this.getSessionId(sessionKey);
        if(sessionId == null) {
            LoggerUtils.debug(getClass(),"Unable to resolve session ID from SessionKey [%s].  Returning null to indicate a session could not be found.", sessionKey.toString());
            return null;
        } else {
            Session s = this.retrieveSessionFromDataSource(sessionId);
            if(s == null) {
                String msg = "Could not find session with ID [" + sessionId + "]";
                LoggerUtils.debug(getClass(),msg);
                //throw new UnknownSessionException(msg);
                return null;
            } else {
                return s;
            }
        }
    }
}
